package com.everflourish.yeah100.act.markingsystem;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.entity.clazz.ClassManagerModel;

import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/5.
 * 时间：2016/7/5
 */
public class ClassExamAdapter extends RecyclerView.Adapter<ClassExamAdapter.ClassViewHolder> {

    private Context mContext;
    private ClassExamActivity mActivity;
    private LayoutInflater mLayoutInflater;
    private List<ClassManagerModel> mList;

    public ClassExamAdapter(Context mContext, ClassExamActivity mActivity,
                            List<ClassManagerModel> mList) {
        this.mContext = mContext;
        this.mActivity = mActivity;
        this.mList = mList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public ClassViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_teacher_share, parent, false);
        ClassViewHolder viewHolder = new ClassViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ClassViewHolder holder, int position) {
        ClassManagerModel classManagerModel = mList.get(position);
        holder.checkBox.setChecked(classManagerModel.isCheck());
        holder.userNameTv.setText(classManagerModel.getClassManage().getName());
        holder.classCountTv.setVisibility(View.VISIBLE);

        if (classManagerModel.getClassManage().getStudents() == null) {
            holder.classCountTv.setText(classManagerModel.getClassManage().getSize() + "");
        } else {
            holder.classCountTv.setText(classManagerModel.getClassManage().getStudents().size() + "");
        }
        holder.deleteLl.setVisibility(View.GONE);
        holder.checkBox.setOnClickListener(new MyOnclickListener(holder, position, classManagerModel));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyOnclickListener implements View.OnClickListener {

        private ClassViewHolder viewHolder;
        private int position;
        private ClassManagerModel classManagerModel;

        public MyOnclickListener(ClassViewHolder viewHolder, int position, ClassManagerModel classManagerModel) {
            this.viewHolder = viewHolder;
            this.position = position;
            this.classManagerModel = classManagerModel;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.teacher_select_ll:
                    break;
                case R.id.teacher_check_rb:
                    classManagerModel.setIsCheck(viewHolder.checkBox.isChecked());
                    break;
                default:
                    break;
            }
        }
    }

    public class ClassViewHolder extends RecyclerView.ViewHolder {

        private CheckBox checkBox;
        private TextView userNameTv;
        private LinearLayout deleteLl;
        private TextView classCountTv;

        public ClassViewHolder(View itemView) {
            super(itemView);
            checkBox = (CheckBox) itemView.findViewById(R.id.teacher_check_rb);
            userNameTv = (TextView) itemView.findViewById(R.id.teacher_name_tv);
            deleteLl = (LinearLayout) itemView.findViewById(R.id.teacher_delete_ll);
            classCountTv = (TextView) itemView.findViewById(R.id.class_count_tv);
        }
    }
}